/*
 * $Id$
 *
 * Copyright 2008 HuangHuang. All rights reserved.
 */
package com.crm.web.page;

import org.apache.tapestry.html.BasePage;
import org.apache.tapestry.annotations.InjectState;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.IPage;
import org.apache.log4j.Logger;
import com.javaforge.tapestry.spring.annotations.InjectSpring;
import com.crm.service.IProcessService;
import com.crm.model.Process;
import com.crm.model.*;
import com.crm.web.util.Tools;

import java.util.List;
import java.util.Date;
import java.util.ArrayList;

/**
 * 沟通提醒信息列表页面类。
 *
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public abstract class ProcessRemind extends BasePage {

    private static Logger logger = Logger.getLogger(ProcessRemind.class);
    private User pageSelectedUser = null;
    private int count;
    private boolean returnFlag;

    @InjectSpring("processService")
    public abstract IProcessService getProcessService();

    @InjectState("userSession")
    public abstract User getUserSession();

    @InjectPage("ProcessModify")
    public abstract ProcessModify getProcessModifyPage();

    public void setPageSelectedUser(User pageSelectedUser) {
        this.pageSelectedUser = pageSelectedUser;
    }

    public boolean isReturnFlag() {
        return returnFlag;
    }

    public String getUserTruename() {
        return pageSelectedUser != null ? pageSelectedUser.getTruename() : "";
    }

    public String goback() {
        pageSelectedUser = null;
        return "UserRemind";
    }

    public String getRecordCount() {
        return "共" + count + "条记录";
    }

    public List getEntries() {
        try {
            List allNoHandleInfo;
            if (pageSelectedUser == null) {
                allNoHandleInfo = getProcessService().getProcessByNoHandle(getUserSession());
                returnFlag = false;
            } else {
                allNoHandleInfo = getProcessService().getProcessByNoHandle(pageSelectedUser);
                returnFlag = true;
            }

            String currentSystemDate = Tools.formatDate(new Date(), Tools.DATEANDTIME);
            currentSystemDate = currentSystemDate.trim();

            List<Process> remindInfoList = new ArrayList<Process>();
            for (Object anAllNoHandleInfo : allNoHandleInfo) {
                Process process = (Process) anAllNoHandleInfo;
                String remindDate = process.getRemindDate().trim();

                Date remindDateObj = Tools.getDateByString(remindDate);
                Date currentDateObj = Tools.getDateByString(currentSystemDate);
                int compareValue = remindDateObj.compareTo(currentDateObj);

                if (compareValue == 0) {
                    process.setIsHandle("今日提醒");
                    remindInfoList.add(process);
                }
                if (compareValue < 0) {
                    long interval = Tools.getDateInterval(currentSystemDate, remindDate);
                    process.setIsHandle("过期" + interval + "天");
                    remindInfoList.add(process);
                }
            }
            count = remindInfoList.size();
            return remindInfoList;
        }
        catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    public IPage onShowDetails(com.crm.model.Process process) {
        ProcessModify page = getProcessModifyPage();
        page.setProcess(process);
        page.setSourcePage(this.getPageName());
        return page;
    }
}

